<?php
namespace Common\Extend\Api;
use Common\Extend\Api\HttpClient;
final class GetData
{
    private $PostMode = array('GET' => 'getSend' , 'POST' => 'postSend' , 'POSTIMG' => 'postImageSend');
    private  $Data;
    public $Mode        =   'GET';
    public $Url         =   '';
    public $postData   =  array();
    public $Charset     =   'UTF-8';
    public $format      =   'json';
    protected $HttpClient;

    public function __construct ()
    {
        $this->HttpClient = new HttpClient;
    }
    public function data(){
        $PostMode = $this->PostMode;
        $ReadMode= $PostMode[$this->Mode];
            if ($ReadMode == 'postImageSend') {
                $this->Data = $this->$ReadMode();
            } else {
                $this->Data = $this->$ReadMode();
            }
        return $this->getArrayData();

    }

    protected function getSend(){
        $data = $this->HttpClient->getfetch($this->Url);
        return $this->Data = $data;
    }
    protected function postSend(){
        $data = $this->HttpClient->postfetch($this->Url,$this->postData);//print_r($data);exit;
        return $this->Data = $data;
    }

    private function FormatApiData($data)
    {
        if(strtoupper($this->Charset) != 'UTF-8')
        {
            if(function_exists('mb_convert_encoding'))
            {
                $data = str_replace('utf-8',$this->Charset,$data);
                $data = @mb_convert_encoding($data,$this->Charset,'UTF-8');
            }elseif(function_exists('iconv'))
            {
                $data = str_replace('utf-8',$this->Charset,$data);
                $data = @iconv('UTF-8',$this->Charset,$data);
            }
        }
        return $data;
    }
    private function get_object_vars_final ($obj)
    {
        if (is_object($obj)) {
            $obj = get_object_vars($obj);
        }

        if (is_array($obj)) {
            foreach ($obj as $key => $value) {
                $obj[$key] = $this->get_object_vars_final($value);
            }
        }
        return $obj;
    }

    private function get_object_vars_final_coding ($obj)
    {
        foreach($obj as $key => $value)
        {
            if(is_array($value))
            {
                $obj[$key] = $this->get_object_vars_final_coding($value);
            }else{
                $obj[$key] = $this->FormatApiData($value);
            }
        }
        return $obj;
    }



    /*public function getXmlData ()
    {
        if (empty($this->Data)) {
            return false;
        }
        return $this->FormatApiData($this->Data);
    }

    public function getJsonData ()
    {
        if (empty($this->Data)) {
            return false;
        }
        if (substr($this->Data, 0, 1) != '{') {

            if ($this->format == 'xml') {
                $Charset = $this->Charset;
                $this->Charset = "UTF-8";
                $Data = $this->getArrayData($this->Data);
                $this->Charset = $Charset;
            }

            $Data = json_encode($Data);
            if (strpos($_SERVER['SERVER_SIGNATURE'], "Win32") > 0) {
                $Data = preg_replace("#\\\u([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])#ie", "iconv('UCS-2','UTF-8',pack('H4', '\\1\\2'))", $Data);
            } else {
                $Data = preg_replace("#\\\u([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])#ie", "iconv('UCS-2','UTF-8',pack('H4', '\\2\\1'))", $Data);
            }
            $Data = $this->FormatApiData($Data);

        } else {
            $Data = $this->Data;
        }
        return $Data;
    }*/
    ///得出的数据
    public function getArrayData (){
        if (empty($this->Data)) {
            return false;
        }

        if ($this->format == 'json') {
            $json = json_decode($this->Data, true);
            return isset($json['rsp']) ? $json['rsp'] : $json;
        } elseif ($this->format == 'xml') {
            $xmlCode = simplexml_load_string($this->Data, 'SimpleXMLElement', LIBXML_NOCDATA);
            $ApiData = $this->get_object_vars_final($xmlCode);
            if(strtoupper($this->Charset) != 'UTF-8'){
                $ApiData = $this->get_object_vars_final_coding($ApiData);
            }
            //类型过滤
            //$this->_ArrayModeData[$this->Charset] = $ApiData;
            return $ApiData;
        } elseif($this->format == 'html') {
            return $this->Data;
        }else {
            return false;
        }
    }

    public function submit(){
        echo $this->postData;
        return true;
    }



}